<template>
  <div>
    <!-- 面包屑导航栏 -->
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>商品管理</el-breadcrumb-item>
      <el-breadcrumb-item>商品分类</el-breadcrumb-item>
    </el-breadcrumb>

    <!-- 卡片视图 -->
    <el-card>
      <el-row>
        <el-button type="primary" @click="showAddCateDialog">添加按钮</el-button>
      </el-row>

      <!-- 商品列表 -->
      <tree-table :data="cateLists" :columns="columns" border :selection-type="false" :expand-type="false" :show-index="true" index-text="#" class="tree-table">
        <!-- 是否有效 -->
        <template slot="isok" slot-scope="scope">
          <i class="el-icon-circle-check" v-if="scope.row.cat_deleted ==false" style="color: lightgreen"></i>
          <i class="el-icon-circle-close" v-else style="color: red"></i>
        </template>
        <!-- 排序 -->
        <template slot="order" slot-scope="scope">
          <el-tag size="mini" v-if="scope.row.cat_level == 0">一级</el-tag>
          <el-tag size="mini" type="warning" v-else-if="scope.row.cat_level == 1">二级</el-tag>
          <el-tag size="mini" type="danger" v-else>三级</el-tag>
        </template>
        <!-- 操作 -->
        <template slot="opt" slot-scope="scope">
          <el-button type="warning" icon="el-icon-edit" size="mini">编辑</el-button>
          <el-button type="danger" icon="el-icon-delete" size="mini">删除</el-button>
        </template>
      </tree-table>

      <!-- 分页 -->
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="categoriesParameter.pagenum"
        :page-sizes="[5,10,20,40]"
        :page-size="categoriesParameter.pagesize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
      ></el-pagination>
    </el-card>

    <!-- 添加按钮对话框 -->
    <el-dialog title="添加分类" :visible.sync="addCateDialogVisible" width="50%" @close="addCateDialogClosed">
      <el-form :model="addCateForm" ref="addCateFormRef" :rules="addCateFormRules" label-width="100px">
        <el-form-item label="分类名称:" prop="cat_name">
          <el-input v-model="addCateForm.cat_name"></el-input>
        </el-form-item>
        <el-form-item label="父级分类：">
          <el-cascader v-model="CascaderValue" :options="parentSortLists" :props="CascaderProps" @change="CascaderHandleChange" clearable></el-cascader>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="addCateDialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="addCate">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
export default {
  created() {
    this.getCateLists();
  },
  data() {
    return {
      // axios请求参数
      categoriesParameter: {
        type: 3,
        pagenum: 1,
        pagesize: 5,
      },
      cateLists: [], // 三层商品分类数据
      total: 0, //总条数
      //   为商品列表tree-table标签各列定义
      columns: [
        {
          label: "分类名称",
          prop: "cat_name",
        },
        {
          label: "是否有效",
          type: "template",
          template: "isok",
        },
        {
          label: "排序",
          type: "template",
          template: "order",
        },
        {
          label: "操作",
          type: "template",
          template: "opt",
        },
      ],
      //   添加对话框关闭
      addCateDialogVisible: false,
      //   添加对话框表单数据
      addCateForm: {
        // 将要添加的分类的名称
        cat_name: "",
        // 父级分类的Id
        cat_pid: 0,
        // 分类的等级，默认要添加的是1级分类
        cat_level: 0,
      },
      // 添加对话框表单验证
      addCateFormRules: {
        cat_name: [
          { required: true, message: "请输入分类名称", trigger: "blur" },
        ],
      },
      parentSortLists: [], //   一二级父级分类列表
      CascaderValue: [], //级联选择器选择的数据
      //   props属性参数
      CascaderProps: {
        value: "cat_id",
        label: "cat_name",
        children: "children",
        checkStrictly: true,
      },
    };
  },
  methods: {
    // 获取商品分类数据
    getCateLists() {
      this.$http.get("categories", { params: this.categoriesParameter }).then((res) => {
        this.cateLists = res.data.data.result;
        this.total = res.data.data.total;
      });
    },
    // 分页功能方法
    handleSizeChange(newSize) {
      this.categoriesParameter.pagesize = newSize;
      this.getCateLists();
    },
    handleCurrentChange(newCurrent) {
      this.categoriesParameter.pagenum = newCurrent;
      this.getCateLists();
    },
    // 打开对话框
    showAddCateDialog() {
      this.getParentSortLists();
      this.addCateDialogVisible = true;
    },
    // 获取父级分类列表
    getParentSortLists() {
      this.$http.get("categories", { params: { type: 2 } }).then((res) => {
        this.parentSortLists = res.data.data;
        console.log(res.data.data);
      });
    },
    // 级联选择器选中后执行(判断分类名称添加到几级分类)
    CascaderHandleChange() {
      if (this.CascaderValue.length > 0) {
        this.addCateForm.cat_pid = this.CascaderValue[this.CascaderValue.length - 1];
        this.addCateForm.cat_level = this.CascaderValue.length;
      } else {
        this.addCateForm.cat_pid = 0;
        this.addCateForm.cat_level = 0;
      }
    },
    // 最后添加新的分类确认
    addCate() {
      this.$refs.addCateFormRef.validate((validate) => {
        if (!validate) {
          return this.$message.error("请完善表单数据再提交确认");
        }
        this.$http.post("categories", this.addCateForm).then((res) => {
          if (res.data.meta.status !== 201) {
            return this.$message.error("添加分类失败，请重新添加");
          }
          this.getCateLists();
          this.$message.success("添加分类成功");
          this.addCateDialogVisible = false;
        });
      });
    },
    // 关闭对话框
    addCateDialogClosed() {
      this.$refs.addCateFormRef.resetFields()
      this.CascaderValue = []
    }
  },
};
</script>

<style scoped>
.tree-table {
  margin: 15px 0;
}
.el-cascader {
  width: 100%;
}
</style>
